package com.wangfugui.timetransfe;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.ui.Messages;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: masiyi
 * @Describe:
 */
public class TimeTransfe extends AnAction {


    private String getClipboardContent() {
        try {
            Toolkit toolkit = Toolkit.getDefaultToolkit();
            Clipboard clipboard = toolkit.getSystemClipboard();
            Transferable transferable = clipboard.getContents(null);

            if (transferable != null && transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                return (String) transferable.getTransferData(DataFlavor.stringFlavor);
            }
        } catch (Exception ex) {
            // 忽略异常，返回null

        }
        return null;
    }

    @Override
    public void actionPerformed(AnActionEvent e) {
        // 获取系统剪贴板内容
        String clipboardContent = getClipboardContent();

        if (clipboardContent != null && !clipboardContent.trim().isEmpty()) {
            try {
                // 自动处理剪贴板内容
                long timestamp = parseTimestamp(clipboardContent.trim());
                Date date = new Date(timestamp);
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String formattedDate = formatter.format(date);

                Messages.showMessageDialog(
                        e.getProject(),
                        "转换结果: " + formattedDate,
                        "时间转换结果",
                        Messages.getInformationIcon()
                );

                System.out.println("自动转换: 输入时间戳 " + timestamp + " -> " + formattedDate);
            } catch (NumberFormatException ex) {
                // 剪贴板内容无效，弹出输入框
                handleManualInput(e);
            }
        } else {
            // 剪贴板为空，弹出输入框
            handleManualInput(e);
        }
    }

    private void handleManualInput(AnActionEvent e) {
        String input = Messages.showInputDialog(
                e.getProject(),
                "请输入时间戳（支持10位秒级或13位毫秒级）:",
                "时间戳转换器",
                Messages.getQuestionIcon()
        );

        if (input != null && !input.isEmpty()) {
            try {
                long timestamp = parseTimestamp(input);
                Date date = new Date(timestamp);
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String formattedDate = formatter.format(date);

                Messages.showMessageDialog(
                        e.getProject(),
                        "转换结果: " + formattedDate,
                        "时间转换结果",
                        Messages.getInformationIcon()
                );

                System.out.println("手动输入: 输入时间戳 " + timestamp + " -> " + formattedDate);
            } catch (NumberFormatException ex) {
                Messages.showMessageDialog(
                        e.getProject(),
                        "请输入有效的时间戳数字（10位或13位）!",
                        "输入错误",
                        Messages.getErrorIcon()
                );
                System.out.println("无效的时间戳输入: " + input);
            }
        } else {
            System.out.println("未输入任何内容或已取消");
        }
    }


    private long parseTimestamp(String input) throws NumberFormatException {
        input = input.trim();
        if (input.length() == 10) {
            // 10位秒级时间戳，转换为毫秒
            return Long.parseLong(input) * 1000;
        } else if (input.length() == 13) {
            // 13位毫秒级时间戳
            return Long.parseLong(input);
        } else {
            throw new NumberFormatException("时间戳长度必须为10位或13位");
        }
    }
}
